package zara
class IpController {
    def authenticateService;
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort ="lastTime"
        params.order = "desc"
        if (params.source) { // 显示最近登录的ip，为了管理终端
                [ipInstanceList: Ip.findAllBySource(params.source, params), ipInstanceTotal: 10]
        }else if (params.ip) { // 显示最近登录的ip，为了管理终端

            log.info("search Ip")
            def calendar=Calendar.getInstance();   

            calendar.add(Calendar.HOUR_OF_DAY ,1)
            def end = calendar.getTime()
            calendar.add(Calendar.HOUR_OF_DAY ,-25)
            def start = calendar.getTime()

          
            [ipInstanceList: Ip.findByAddressAndLastTimeBetween(params.ip , start , end), 
                ipInstanceTotal: Ip.countByAddressAndLastTimeBetween(params.ip , start , end)]
        }
        else{
        [ipInstanceList: Ip.list(params), ipInstanceTotal: Ip.count()]
        }
    }

    def create = {
        def ipInstance = new Ip()
        ipInstance.properties = params
        return [ipInstance: ipInstance]
    }

    def save = {
        def ipInstance = new Ip(params)
        if (ipInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'ip.label', default: 'Ip'), ipInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [ipInstance: ipInstance])
        }
    }

    def show = {
        def ipInstance = Ip.get(params.id)
        if (!ipInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'ip.label', default: 'Ip'), params.id])}"
            redirect(action: "list")
        }
        else {
            [ipInstance: ipInstance]
        }
    }

    def edit = {
        def ipInstance = Ip.get(params.id)
        if (!ipInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'ip.label', default: 'Ip'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [ipInstance: ipInstance]
        }
    }

    def update = {
        def ipInstance = Ip.get(params.id)
        if (ipInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (ipInstance.version > version) {
                    
                    ipInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'ip.label', default: 'Ip')] as Object[], "Another user has updated this Ip while you were editing")
                    render(view: "edit", model: [ipInstance: ipInstance])
                    return
                }
            }
            ipInstance.properties = params
            if (!ipInstance.hasErrors() && ipInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'ip.label', default: 'Ip'), ipInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [ipInstance: ipInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'ip.label', default: 'Ip'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def ipInstance = Ip.get(params.id)
        if (ipInstance) {
            try {
                ipInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'ip.label', default: 'Ip'), ipInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'ip.label', default: 'Ip'), ipInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'ip.label', default: 'Ip'), params.id])}"
            redirect(action: "list")
        }
    }

    def batchDelete = {
            log.info("batch delete Ip")
            def calendar=Calendar.getInstance();   

            calendar.add(Calendar.DAY_OF_YEAR  ,-3)
            
             
            Ip.executeUpdate("delete Ip ip where ip.lastTime < ?", [calendar.getTime()])

            redirect(action: "list")

    }

    def flushOther = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort ="lastTime"
        params.order = "desc"
        [ipInstanceList: Ip.findAllByPerson(authenticateService.userDomain(), params), ipInstanceTotal: Ip.countByPerson(authenticateService.userDomain())]
    }

    def flushMe = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.sort ="lastTime"
        params.order = "desc"
        [ipInstanceList: Ip.findAllBySitePerson(authenticateService.userDomain(), params), ipInstanceTotal: Ip.countBySitePerson(authenticateService.userDomain())]
    }
}
